package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.dto.BindTeachplanWorkDto;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.service.TeachplanMediaService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程计划前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/teachplan")
public class TeachplanController {

    @Autowired
    private TeachplanService  teachplanService;
    @Autowired
    private TeachplanMediaService teachplanMediaService;
    /**
     * 查询 课程大纲
     * @param courseId 课程id
     * @return vo{课程大纲 相关媒资}
     */
    @GetMapping("/{courseId}/tree-nodes")
    public ResponseResult treeNodes(@PathVariable Long courseId){
        return teachplanService.treeNodes(courseId);
    }

    /**
     * 新增/修改 课程大纲
     * @param teachplanDto 课程大纲修改内容
     * @return vo
     */
    @PostMapping
    public ResponseResult saveOrUpdate(@RequestBody TeachplanDto teachplanDto){
        return teachplanService.saveOrUpdate(teachplanDto);
    }

    /**
     * 删除 课程大纲章节
     * @param id 章节id
     * @return null
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id){
        return teachplanService.deleteById(id);
    }

    /**
     * 课程计划绑定 媒资
     * POST /media/association
     * @param dto BindTeachplanMediaDto
     * @return vo
     */
    @PostMapping("/media/association")
    public ResponseResult associateMedia(@RequestBody BindTeachplanMediaDto dto) {
        return teachplanService.associateMedia(dto);
    }

    /**
     * 根据id查询课程及计划
     * @param teachplanId id
     * @return TeachplanVo
     */
    @GetMapping("/{teachplanId}")
    public ResponseResult findPlanById(@PathVariable("teachplanId") Long teachplanId){
        Teachplan teachplan = teachplanService.getById(teachplanId);
        TeachplanVo vo = BeanHelper.copyProperties(teachplan, TeachplanVo.class);
        return ResponseResult.okResult(vo);
    }

    /**
     * 课程计划绑定 作业
     * POST /work/association
     * @param dto BindTeachplanWorkDto
     * @return vo
     */
    @PostMapping("/work/association")
    public ResponseResult associateWork(@RequestBody BindTeachplanWorkDto dto) {
        return teachplanService.associateWork(dto);
    }

    /**
     * 课程计划解绑作业
     * @param teachplanWorkId 课程-作业绑定ID
     * @param courseBaseId 课程id
     * @return boolean
     */
    @DeleteMapping("/work/{teachplanWorkId}")
    public ResponseResult unbind(@PathVariable("teachplanWorkId") Long teachplanWorkId,Long courseBaseId){
        return  teachplanService.unbind(teachplanWorkId,courseBaseId);
    }
}
